﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Sale
{
    public partial class Sale_VPlantChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBaseHelper.BindDrp(Drp_STATUS, EnumData.DataBaseEunm.RemindstrSTATUS);
                DataBaseHelper.BindDrp(Drp_VITYPE, EnumData.DataBaseEunm.RemindstrTypeTwo);

                txt_CUTNAM.OnClientTriggerClick = Window1.GetSaveStateReference(txt_CUTNUB.ClientID, txt_CUTNAM.ClientID, txt_ADDRES.ClientID, txt_CONTAT.ClientID, txt_TELNUB.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectCustom.aspx?r=3", "选择客户");

                txt_VIPERSON.OnClientTriggerClick = Window1.GetSaveStateReference(hid_VIPERSON.ClientID, txt_VIPERSON.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectUsers.aspx", "选择拜访人员");
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                LoadData();
                if (!string.IsNullOrEmpty(Request["s"]))
                {
                    btnSave.Hidden = true;

                }
            }
        }
        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["cid"]))
            {
                Proj_Custor SV = TbsDbSession.Default.From<Proj_Custor>().Where(Proj_Custor._.ID == int.Parse(Request["cid"])).ToFirst();
                if (SV != null)
                {
                    txt_CUTNUB.Text = SV.CUTNUB;

                    txt_CUTNAM.Text = SV.CUTNAM;
                    txt_CONTAT.Text = SV.CONTAT;
                    txt_ADDRES.Text = SV.ADDRES;
                    txt_TELNUB.Text = SV.TELPHO;
                }
            }
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Sale_VPlant SV = TbsDbSession.Default.From<Sale_VPlant>().Where(Sale_VPlant._.ID == int.Parse(Request["id"])).ToFirst();
                if (SV != null)
                {
                    txt_CUTNUB.Text = SV.CUTNUB;
                    txt_CUTNAM.Text = SV.CUTNAM;
                    txt_CONTAT.Text = SV.CONTAT;
                    txt_POSITION.Text = SV.POSITION;
                    Drp_VITYPE.SelectedValue = SV.VITYPE;
                    txt_VIPERSON.Text = SV.VIPERSON;
                    txt_ADDRES.Text = SV.ADDRES;
                    txt_OBJECTIVE.Text = SV.OBJECTIVE;
                    txt_SURVRY.Text = SV.SURVRY;
                    txt_VCONTEBT.Text = SV.VCONTEBT;
                    txt_STIME.Text = SV.STIME.Value.ToString();
                    txt_ETIME.Text = SV.ETIME.Value.ToString();
                    txt_TELNUB.Text = SV.TELNUB;
                    Drp_STATUS.SelectedValue = SV.STATUS.ToString();
                    txt_REMARK.Text = SV.REMARK;
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Sale_VPlant SV = new Sale_VPlant();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                SV.ID = int.Parse(Request["id"]);
                SV.Attach();
                SV.CUTNUB = txt_CUTNUB.Text;
                SV.CUTNAM = txt_CUTNAM.Text;
                SV.CONTAT = txt_CONTAT.Text;
                SV.POSITION = txt_POSITION.Text;
                SV.VITYPE = Drp_VITYPE.SelectedValue;
                SV.VIPERSON = txt_VIPERSON.Text;
                SV.ADDRES = txt_ADDRES.Text;
                SV.OBJECTIVE = txt_OBJECTIVE.Text;
                SV.SURVRY = txt_SURVRY.Text;
                SV.VCONTEBT = txt_VCONTEBT.Text;
                SV.STIME = Convert.ToDateTime(txt_STIME.Text);
                SV.ETIME = Convert.ToDateTime(txt_ETIME.Text);
                SV.TELNUB = txt_TELNUB.Text;
                SV.STATUS = Drp_STATUS.SelectedValue;
                SV.REMARK = txt_REMARK.Text;
                SV.CONTAT = txt_CONTAT.Text;
                returnvalue = TbsDbSession.Default.Update<Sale_VPlant>(SV);
            }
            else
            {
                int maxid = TbsDbSession.Default.From<Sale_VPlant>().Select(Sale_VPlant._.ID.Max()).ToScalar<int>();
                if (maxid == 0)
                    maxid = 1;
                else
                    maxid = maxid + 1;
                SV.DeAttach();
                SV.VPID = (1000000 + maxid).ToString();
                SV.CUTNUB = txt_CUTNUB.Text;
                SV.CUTNAM = txt_CUTNAM.Text;
                SV.CONTAT = txt_CONTAT.Text;
                SV.POSITION = txt_POSITION.Text;
                SV.VITYPE = Drp_VITYPE.SelectedValue;
                SV.VIPERSON = txt_VIPERSON.Text;
                SV.ADDRES = txt_ADDRES.Text;
                SV.OBJECTIVE = txt_OBJECTIVE.Text;
                SV.SURVRY = txt_SURVRY.Text;
                SV.VCONTEBT = txt_VCONTEBT.Text;
                SV.STIME = Convert.ToDateTime(txt_STIME.Text);
                SV.ETIME = Convert.ToDateTime(txt_ETIME.Text);
                SV.TELNUB = txt_TELNUB.Text;
                SV.STATUS = Drp_STATUS.SelectedValue;
                SV.REMARK = txt_REMARK.Text;
                SV.AddID = CookInfo.EAdminid();
                SV.Adder = CookInfo.EAdminTrueName();
                SV.AddTime = DateTime.Now;
                SV.CONTAT = txt_CONTAT.Text;
                returnvalue = TbsDbSession.Default.Insert<Sale_VPlant>(SV);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
            }
        }
    }
}